<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/c" prefix="c" %>
<%@taglib prefix="display" uri="http://displaytag.sf.net/el"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="com.solution.webconstant.PermissionConstants" %>
<script type="text/javascript" src="../js/prototype.js"></script>
<%
	HashMap permissionMap = ((HashMap)(session.getAttribute("userPermissionsInfo")));
%>
<script language="JavaScript">
var userArray = new Array();
var userRowID= 1;
function addRow(obj){
	//var div = new Element('div', { 'id': 'hierarchyRow'+hierarchyRowID,'class':'empty'});
	var content = $('userRow').innerHTML;
	
	content = content.replace(/rowId/,""+userRowID);
	if($('first_name_temp').value.strip()=="")
	{
		alert("Please enter First Name");
		$('first_name_temp').focus();
		return;
	}
	if($('last_name_temp').value.strip()=="")
	{
		alert("Please enter Last Name");
		$('last_name_temp').focus();
		return;
	}
	if($('user_name_temp').value.strip()=="")
	{
		alert("Please enter User Name");
		$('user_name_temp').focus();
		return;
	}
	if(examNameExist){
		alert("Please enter Uniue User Name");
		$('user_name_temp').focus();
		return;
	}
	if($('password_temp').value.strip()=="")
	{
		alert("Please enter password");
		$('password_temp').focus();
		return;
	}
	var pattern = /^[1-9]([0-9]{9})$/;
	if($('mobile_temp').value.strip()!=""){
		if(!pattern.test($('mobile_temp').value)){
			alert("Please enter Valid mobile number");
			$('mobile_temp').focus();
			return;
		}
	}
	
	content = content.replace(/firstName/,$('first_name_temp').value);
	content = content.replace(/lastName/,$('last_name_temp').value);
	content = content.replace(/userName/,$('user_name_temp').value);
	content = content.replace(/password/,"********");

	
	content = content.replace(/firstNameValue/,$('first_name_temp').value);
	content = content.replace(/lastNameValue/,$('last_name_temp').value);
	content = content.replace(/userNameValue/,$('user_name_temp').value);
	content = content.replace(/passwordValue/,$('password_temp').value);
	content = content.replace(/empcodeValue/,$('emp_code_temp').value);
	content = content.replace(/mobileValue/,$('mobile_temp').value);
	content = content.replace(/emailValue/,$('email_temp').value);
	content = content.replace(/genderValue/,$('gender_temp').value);
	content = content.replace(/userRowData/,"1");

	content = content.replace(/user_name_array_id/,"user_name_array_id"+userRowID);
	if(userArray[$('user_name_temp').value]!= "added") {
		userArray[$('user_name_temp').value] = "added";
	}else{
		alert("User Name Already added");
		return;
	}
	$('first_name_temp').value='';
	$('last_name_temp').value='';
	$('user_name_temp').value='';
	$('password_temp').value='';
	$('emp_code_temp').value='';
	$('mobile_temp').value='';
	$('email_temp').value='';
	$('gender_temp').value='Male';
	
	content = content.replace(/<TBODY>/i,"");
	content = content.replace(/<\/TBODY>/i,"");
	$('userHeaderRow').style.display = "";
	$('userHeaderRowContainer').style.display = "";	
	userRowID++;
	$('temp').insert({after:content});	
}
function removeRow(obj){
	userArray[$('user_name_array_id'+obj.id).value]= "";
	$(obj.id).up(1).remove();
	userRowID--;
}
var examNameExist = false;
function isNameExist(){
	var userName = $('user_name_temp').value;
	new Ajax.Request('/isUserNameExist.do?user_name='+userName,   
			{     method:'get',     
				  onSuccess: function(transport)
					{       
						var response = transport.responseText || "no response text";
						if(response=="TRUE"){
							examNameExist=  true;
							$('userNameId').innerHTML = "User name exist!"
							$('userNameId').style.display = "";
						}else{
							examNameExist = false;
							$('userNameId').innerHTML="";
							$('userNameId').style.display = "none";
						}
					},     
				  onFailure: function()
				    { 
						alert('Something went wrong...') 
					}   
			}
		); 
}
function fnSubmit()
{
	var userTypeArray = document.getElementsByName("user_type");
	if(!userTypeArray[0].checked && !userTypeArray[1].checked){
		alert("Please select User type.");		
		return;
	}
	if(!userTypeArray[0].checked){	
		if($('exam_type').value==""){
			alert("Please select exam type.");
			$('exam_type').focus();
			return;
		}
	}
	
	if(userRowID==1){
		alert("Please Add User using 'Add User' button.");
		return;
	}
	$('userRowData').innerHTML = '';	
	document.userForm.action ='/addUser.do?';
	document.userForm.submit();
}
function fnEditViewUserDetails(exam_id_value, screen_type_value){
	document.userForm.screen_type.value=screen_type_value;
	document.userForm.user_id.value=exam_id_value;
	document.userForm.action='/editViewUser.do?';
	document.userForm.submit();
}
function clearForm(formObj){
	var elements = formObj.elements;
	for(i=0; i<elements.length; i++){
		if(elements[i].type == "text")
			elements[i].value = "";
		if(elements[i].type == "select-one" || elements[i].type == "select-multiple")
			elements[i].selectedIndex = 0;
	}
}
function enableDisableExamType(userType){
	if(userType=='1'){
		$('exam_type').disabled = true;
	}else{
		$('exam_type').disabled = false;
	}

}

</script>
<script language="JavaScript" src="js/Calendar.js" type="text/JavaScript"></script>
<html:form action="/user">
	<html:hidden property="user_id" />
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  	<tr>
    	<td>
    		<table width="100%" border="0" cellspacing="0" cellpadding="0">
  				<tr>
  					<jsp:include page="userActionTab.jsp"/>	
  				</tr>
			</table>
		</td>
  	</tr>
  	<tr>
    	<td class="formbg" valign="top">
    		<table width="100%" border="0" cellspacing="0" cellpadding="0">
  				<tr>
    				<td>
    					<table width="100%" border="0" cellspacing="4" cellpadding="0">
    						<tr>
								<td>&nbsp;</td>	
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
  							<tr>
    							<td colspan="6"><jsp:include page="showErrorMessage.jsp"/></td>
  							</tr>  
							<tr>
							  <td>&nbsp;</td>
							  <td class="formText">First Name<font class="asterix">*</font></td>
							  <td ><input name="first_name_temp" id="first_name_temp" class="form1" maxlength="20"/></td>
							  <td class="formText">Last Name<font class="asterix">*</font></td>
							  <td><input name="last_name_temp" id="last_name_temp" class="form1" maxlength="20"/></td>
							</tr>
							<tr>
							  <td>&nbsp;</td>
							  <td class="formText">User Name<font class="asterix">*</font></td>
							  <td ><input name="user_name_temp" id="user_name_temp" class="form1" maxlength="20" onblur="javascript:isNameExist();"/><div id="userNameId" class="errorMsg" style="display:none;"  ></div></td>
							  <td class="formText">Password<font class="asterix">*</font></td>
							  <td><input name="password_temp" type="password" id="password_temp" maxlength="20" class="form1" /></td>
							</tr>
							<tr>
							  <td>&nbsp;</td>
							  <td class="formText">Emp. Code/ Role no</td>
							  <td ><input name="emp_code_temp" id="emp_code_temp" class="form1" maxlength="10"/></td>
							  <td class="formText">Mobile</td>
							  <td><input name="mobile_temp" id="mobile_temp" class="form1" maxlength="10"/></td>
							</tr>
							<tr>
							  <td>&nbsp;</td>
							  <td class="formText">Email ID</td>
							  <td ><input name="email_temp" id="email_temp" class="form1" maxlength="25"/></td>
							  <td class="formText">Gender</td>
							  <td><select Class="form2" property="gender_temp" id="gender_temp" >
								<option value="male">Male</option>
								<option value="female">Female</option>
								</select></td>
							</tr>
							<tr>
							  	<td>&nbsp;</td>
							  	<td colspan="5" align="center" ><input type="button" class="imageButton" id="addUserButton" value="Add User" onClick="javascript:addRow($('temp'))"></td>
							</tr>
  							<tr>
								<td height="25" colspan="6" valign="top"  >
  									<table border="0" bgcolor="#dad9f7" cellspacing="0" cellpadding="0" id="userHeaderRowContainer" style="display:none;">
										<tr>
											<td class="innerBoxCorner1"></td>
										  	<td bgcolor="#a4a3a7"></td>
										  	<td class="innerBoxCorner2"></td>
										</tr>
      									<tr>
        									<td bgcolor="#a4a3a7"></td>
        									<td>
        										<table width="100%" border="0" bgcolor="#a4a3a7" cellspacing="0" cellpadding="0">
		  											<tr>
														<td class="formText" width="800px">
			  												<table width="100%" border="0" cellspacing="0" cellpadding="0" class="innerTable" id="userHeaderRow" style="display:none;">
																<tr id="temp">
																	<td >&nbsp;</td>
																	<td class="formText" >First Name</td>
																	<td class="formText" >Last Name</td>
																	<td class="formText" >User Name</td>
																	<td class="formText" colspan="2" >Password</td>
																</tr>
															</table>
														</td>
		  											</tr>
		  										</table>
		  									</td>
		 									<td bgcolor="#a4a3a7"></td>
		 								</tr>
	  									<tr>
        									<td class="innerBoxCorner4"></td>
        									<td bgcolor="#a4a3a7"></td>
        									<td class="innerBoxCorner3"></td>
      									</tr>
	  								</table>
	 							</td>
							</tr>
  							<tr>
								<td height="10" colspan="6" valign="top" ></td>
							</tr>	
							<tr>
								<td class="formText" colspan="3"><strong>User Type</strong></td>
								<td colspan="4"></td>
							</tr>
							<tr>
								<td height="10" colspan="4" valign="top"  ></td>
							</tr>
							<tr>
								<td><html:radio name="userForm" property="user_type" styleId="user_type_examiner" value="1" onclick="javascript:enableDisableExamType('1');" /></td>
								<td class="formText" colspan="5">Examiner</td>
							</tr>
							<tr>
								<td><html:radio name="userForm" property="user_type" styleId="user_type_examinee" value="2" onclick="javascript:enableDisableExamType('2');"/>
								</td>
								<td class="formText" colspan="5">Examinee</td>
							</tr>
							<tr>
								<td align="left" valign="top" ></td>
								<td align="left" valign="top" class="formText">Exam Type</td>
								<td valign="middle" colspan="4">
									<html:select property="exam_type" styleId="exam_type" styleClass="form2">
									<html:option value="">--Please Select--</html:option>
									<logic:present name="userForm" property="exam_type_list">
									<logic:iterate name="userForm" property="exam_type_list" id="examType" >
									<bean:define id="dID" name="examType" property="id" />
									<html:option  value="<%=dID.toString()%>"><bean:write name="examType" property="name"/></html:option>		
									</logic:iterate>
									</logic:present>
									</html:select>
								</td>	
							</tr>
						</table>
					</td>
  				</tr>
  				<tr>
    				<td width="100%">
    					<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td height="25" colspan="3" valign="top"  >&nbsp;</td>
							</tr>
							<tr>
								<td height="25" colspan="3" valign="top"><div align="center">
									<input type="button" class="imageButton" value="Submit" onClick="JavaScript:fnSubmit()">
       	 							<input type="button" class="imageButton" value="Reset" onClick="JavaScript:clearForm(this.form)">
                        		</div></td>
							</tr>
							<tr>
							  <td class="lowerCorner1"></td>
							  <td class="lowerbg" width="800px"></td>
							  <td class="lowerCorner2"></td>
							</tr>
    					</table>
    				</td>
  				</tr>
			</table>
		</td>
  	</tr>
  	<tr>
    	<td>&nbsp; </td>
  	</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="userRow" style="display:none;">
	<tr>
		<td  align="left" class="upperLine"><img id="rowId" valign="bottom" src="images/removeIcon.gif" onClick="javascript:removeRow(this);"/></td><td align="left" class="upperLine"> firstName</td>
		<td  align="left" class="upperLine">lastName</td>
		<td  align="left" class="upperLine">userName</td>
		<td  align="left" colspan="2" class="upperLine">password<div  id="userRowData">
		<input type="hidden" name="first_name_array" value="firstNameValue">
		<input type="hidden" name="last_name_array" value="lastNameValue">
		<input type="hidden" name="user_name_array" id="user_name_array_id" value="userNameValue">
		<input type="hidden" name="password_array" id="password_array_id" value="passwordValue">
		<input type="hidden" name="emp_code_array" id="emp_code_array_id" value="empcodeValue">
		<input type="hidden" name="mobile_array" id="mobile_array_id" value="mobileValue">
		<input type="hidden" name="gender_array" id="gender_array_id" value="genderValue">
		<input type="hidden" name="email_array" id="email_array_id" value="emailValue">
		</div>
		</td>
	</tr>
</table>
	 
</html:form>